package com.meihua.baoleyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.common.MyApp;


/**
 * 添加银行卡信息
 * Created by liming on 2017/10/18
 */
public class AddBankInfoActivity extends BaseActivity implements View.OnClickListener {

    /**
     * 界面标题
     */
    private TextView text_title;//标题
    /**
     * 返回键按钮
     */
    private ImageView text_back;//返回
    /**
     * 输入银行卡号输入框
     */
    private EditText add_bank_info_number;//卡号
    /**
     * 输入持卡人姓名输入框
     */
    private TextView add_bank_info_name;//持卡人姓名
    /**
     * 下一步按钮
     */
    private TextView add_bank_info_next;//下一步

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bank_info);
        //初始化界面控件
        initView();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        text_title = (TextView) findViewById(R.id.text_title);
        text_back = (ImageView) findViewById(R.id.text_back);
        add_bank_info_name = (TextView) findViewById(R.id.add_bank_info_name);
        add_bank_info_number = (EditText) findViewById(R.id.add_bank_info_number);
        add_bank_info_next = (TextView) findViewById(R.id.add_bank_info_next);
        //设置界面标题
        text_title.setText("添加银行卡");
        add_bank_info_name.addTextChangedListener(textWatcher);
        add_bank_info_name.setText(MyApp.getInstance().getReal_name());
        add_bank_info_number.addTextChangedListener(textWatcher);
        text_back.setOnClickListener(this);
        add_bank_info_next.setOnClickListener(this);
    }

    /**
     * 界面返回时调用方法
     * @param requestCode 请求code
     * @param resultCode 返回code
     * @param data 传参
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (data.getIntExtra("finish", 2) == 1) {
                finish();
            }
        }
    }

    /**
     * 输入框监听
     */
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!add_bank_info_name.getText().toString().equals("") &&
                    (add_bank_info_number.getText().toString().length() >= 16 &&
                            add_bank_info_number.getText().toString().length() <= 19)) {
                add_bank_info_next.setBackgroundResource(R.drawable.bank_next_bg_black);
                add_bank_info_next.setEnabled(true);
            } else {
                add_bank_info_next.setBackgroundResource(R.drawable.bank_next_bg_e0);
                add_bank_info_next.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };


    /**
     * 绑定事件
     * @param view 被点击的事件
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_bank_info_next://下一步
                Intent in = new Intent(AddBankInfoActivity.this, AddBankInfoPhoneActivity.class);
                in.putExtra("real_name", add_bank_info_name.getText().toString());
                in.putExtra("banks_card",add_bank_info_number.getText().toString().trim());
                startActivityForResult(in, 1);
                break;
            case R.id.text_back://返回
                finish();
                break;
        }
    }
}
